<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {

/**
 * admin_index method
 *
 * @return void
 */
 
	public function admin_logout()
	{
		$this->layout = 'admin_login';
		if($this->Auth->user())
		{
			$this->redirect($this->Auth->logout());
		}
		else
		{
			$this->redirect(array('controller'=>'pages','action' => 'display','home'));
			$this->Session->setFlash(__('No inicio secion'), 'default', array(), 'auth');
		}
	}
	
	public function logout()
	{
		if($this->Auth->user())
		{
			$this->redirect($this->Auth->logout());
		}
		else
		{
			$this->redirect(array('controller'=>'pages','action' => 'display','home'));
			$this->Session->setFlash(__('No inicio secion'), 'default', array(), 'auth');
		}
	}
	
	public function login() {
		
		if ($this->request->is('post')) {
	
			if ($this->Auth->login()) {
				$this->redirect(array('controller' => 'propiedades', 'action' => 'index'));
			} else {
				$this->Session->setFlash(__('Usuario incorrecto, intente nuevamente'));
			}
		}
	}
	function admin_login(){
		//$this->layout = 'admin_login';
		$this->layout='back_end';

		if($this->request->is('post')) {
			if($this->Auth->login()) {
				$this->redirect(array('controller' => 'propiedades', 'action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('Invalid username and password, try again'));
			}
		}
	}
	
 
	public function admin_index() {
		$this->layout='back_end';
		$this->set('title_for_layout', 'Listado de Usuarios');
		$this->set('listTitle', 'Listado de usuarios');       
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		$this->layout='back_end';
		$this->set('title_for_layout', 'Vista de Usuario');
		$this->set('listTitle', 'Vista de Usuario'); 
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		$this->layout='back_end';
		$this->set('title_for_layout', 'Nuevo Usuario');
		$this->set('listTitle', 'Nuevo Usuario'); 	
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->flash(__('User saved.'), array('action' => 'index'));
				$this->redirect(array('action' => 'index'));
			} else {
			}
		}
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		$this->layout='back_end';
		$this->set('title_for_layout', 'Editar Usuario');
		$this->set('listTitle', 'Editar Uvsuario'); 		
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->User->save($this->request->data)) {
				$this->flash(__('The user has been saved.'), array('action' => 'index'));
			} else {
			}
		} else {
			$this->request->data = $this->User->read(null, $id);
		}
	}

/**
 * admin_delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->User->delete()) {
			$this->flash(__('User deleted'), array('action' => 'index'));
		}
		$this->flash(__('User was not deleted'), array('action' => 'index'));
		$this->redirect(array('action' => 'index'));
	}
}
